package org.dubnation.user.controller;

import org.dubnation.auth.domain.vo.LoginVO;
import org.dubnation.user.service.IUserService;
import org.dubnation.user.domain.User;
import org.dubnation.user.query.UserQuery;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dubnation.user.domain.dto.UerRegisterDTO;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
/**
 * 用户相关操作
 */
public class UserController {
    @Autowired
    public IUserService userService;

    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody User user){
        if( user.getId()!=null)
            userService.update(user);
        else
            userService.insert(user);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        userService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        User user = userService.queryById(id);
        return AjaxResult.me().setData(user);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){
        List< User> list = userService.queryAll();
        return AjaxResult.me().setData(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody UserQuery query){
        PageInfo<User> pageInfo = userService.queryPage(query);
        return AjaxResult.me().setData(pageInfo);
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        userService.batchDelete(ids);
        return AjaxResult.me();
    }

    /**
     * 用户注册
     * @param uerRegisterDto
     * @return
     */
    @PutMapping("/register")
    public AjaxResult userRegister(@RequestBody UerRegisterDTO uerRegisterDto) {
        userService.userRegister(uerRegisterDto);
        return AjaxResult.me();
    }

    /**
     * 做一些处理
     * @return
     */
    @GetMapping("/whatever")
    public AjaxResult getSomething() {
        return AjaxResult.me().setData("曼城是冠军!");
    }
}
